<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>机蜂博客</title>
    <base href="<%=request.getContextPath()%>/">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="static/layui/css/layui.css">
    <script src="static/jquery/jquery.js"></script>
    <script src="static/layui/layui.js"></script>
</head>
<body>
<div style="padding: 0 20px">
    <div class="layui-tab layui-tab-brief" lay-filter="loginTab">
        <ul class="layui-tab-title" style="text-align: center">
            <li lay-id="0">登录</li>
            <li lay-id="1">注册</li>
        </ul>
        <div class="layui-tab-content">
            <%--登陆表--%>
            <div class="layui-tab-item">
                <form class="layui-form">
                    <div class="demo-login-container">
                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-username"></i>
                                </div>
                                <input type="text" name="username" value="" lay-verify="required|uname"
                                       placeholder="用户名"
                                       lay-reqtext="请填写用户名" autocomplete="off" class="layui-input"
                                       lay-affix="clear">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-password"></i>
                                </div>
                                <input type="password" name="password" value="" lay-verify="required"
                                       placeholder="密   码" lay-reqtext="请填写密码" autocomplete="off"
                                       class="layui-input" lay-affix="eye">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-col-md6" style="margin-bottom: 15px; width: 360px">
                                <select id="logrole" name="role">
                                    <option value="">请选择身份</option>
                                    <option value="2">用户</option>
                                    <option value="3">配送员</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-row">
                                <div class="layui-col-xs7">
                                    <div class="layui-input-wrap">
                                        <div class="layui-input-prefix">
                                            <i class="layui-icon layui-icon-vercode"></i>
                                        </div>
                                        <input type="text" name="code" value="" lay-verify="required"
                                               placeholder="验证码" lay-reqtext="请填写验证码" autocomplete="off"
                                               class="layui-input" lay-affix="clear">
                                    </div>
                                </div>
                                <div class="layui-col-xs5">
                                    <div style="margin-left: 10px;">
                                        <img style="display: block;height: 38px;width: 100%;" id="kaptchaImage"
                                             alt="加载异常"
                                             title="点击更换" onclick="getKaptcha()">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <a href="javascript:;" onclick="forget()"
                               style="float: right; margin-top: 7px;color: #01aaed">忘记密码？</a>
                        </div>
                        <div class="layui-form-item">
                            <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="loginBtn">登录</button>
                        </div>
                        <div class="layui-form-item" style="text-align: right;">
                            没有账号立即 <a href="javascript:;" onclick="changeTab(1)" style="color: #01aaed">注册</a>
                        </div>
                    </div>
                </form>
            </div>
            <%--注册表--%>
            <div class="layui-tab-item">
                <form class="layui-form" id="registerForm">
                    <div class="demo-reg-container">
                        <%--<div class="layui-form-item">
                            <div class="layui-row">
                                <div class="layui-col-xs7">
                                    <div class="layui-input-wrap">
                                        <div class="layui-input-prefix">
                                            <i class="layui-icon layui-icon-cellphone"></i>
                                        </div>
                                        <input type="text" name="phone" value="" lay-verify="required|phone"
                                               placeholder="手机号" lay-reqtext="请填写手机号" autocomplete="off"
                                               class="layui-input" id="regPhone">
                                    </div>
                                </div>
                                <div class="layui-col-xs5">
                                    <div style="margin-left: 11px;">
                                        <button type="button" class="layui-btn layui-btn-fluid layui-btn-primary"
                                                id="sendMsg">获取验证码
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-vercode"></i>
                                </div>
                                <input type="text" name="code" value="" lay-verify="required" placeholder="验证码"
                                       lay-reqtext="请填写验证码" autocomplete="off" class="layui-input">
                            </div>
                        </div>--%>
                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-username"></i>
                                </div>
                                <input type="text" name="username" value="" lay-verify="required|uname"
                                       placeholder="用户名" lay-reqtext="请填写用户名"
                                       autocomplete="off" class="layui-input" lay-affix="clear">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-password"></i>
                                </div>
                                <input type="password" name="password" value="" lay-verify="required" placeholder="密码"
                                       autocomplete="off" class="layui-input" id="reg-password" lay-affix="eye">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-password"></i>
                                </div>
                                <input type="password" name="confirmPassword" value=""
                                       lay-verify="required|confirmPassword" placeholder="确认密码" autocomplete="off"
                                       class="layui-input" lay-affix="eye">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-wrap">
                                <div class="layui-input-prefix">
                                    <i class="layui-icon layui-icon-username"></i>
                                </div>
                                <input type="text" name="nickname" value="" lay-verify="required" placeholder="昵称"
                                       autocomplete="off" class="layui-input" lay-affix="clear">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-col-md6" style="margin-bottom: 15px; width: 360px">
                                <select id="regrole" name="role">
                                    <option value="">请选择身份</option>
                                    <option value="2">用户</option>
                                    <option value="3">配送员</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="regBtn">注册</button>
                        </div>
                        <div class="layui-form-item" style="text-align: right">
                            已有帐号直接 <a href="javascript:;" style="color: #01aaed" onclick="changeTab(0)">登录</a>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    let element;
    let uuid;//图片验证码uuid
    let codeUUID;//短信验证码uuid
    layui.use(['form', 'element'], function () {
        const form = layui.form;
        element = layui.element;
        changeTab('${type}');
        getKaptcha();
        /*监听注册按钮*/
        form.on('submit(regBtn)', function (data) {
            $.ajax({
                url: "vip/register",
                data: data.field,
                method: "post",
                dataType: "json",
                success(res) {
                    if (res.code === 200) {
                        layer.alert('注册成功，使用用户名密码进行登录！', {icon: 6}, function (index) {
                            document.getElementById("registerForm").reset();
                            changeTab(0);
                            layer.close(index);
                        })
                    } else {
                        layer.alert(res.msg, {icon: 5});
                    }

                }
            });
            return false;
        });

        form.on('submit(loginBtn)', function (data) {
            data.field.uuid = uuid
            $.ajax({
                url: "vip/login",
                data: data.field,
                method: "post",
                dataType: "json",
                success(res) {
                    if (res.code === 200) {
                        layer.msg("登录成功", {icon: 1});
                        if (res.data === 2) {
                            top.location.href = "vip/uhome";
                        } else if (res.data === 3) {
                            top.location.href = "vip/dhome";
                        }

                    } else {
                        getKaptcha();
                        $("#password").val("");
                        $("#code").val("");
                        layer.alert(res.msg, {icon: 2});
                    }
                }
                ,
                error(e) {
                    console.log(e);
                    layer.alert("服务器睡着了...", {icon: 2})
                }
            });
            return false;
        })
        ;

        /*发送短信按钮*/
        /* $("#sendMsg").click(function () {
             const phone = $("#regPhone").val();
             if (!phone) {
                 layer.msg('请输入手机号码!', {icon: 2});
                 return false;
             }
             if (!(/^1\d{10}$/.test(phone))) {
                 layer.msg('手机号码格式错误!', {icon: 2});
                 return false;
             }
             $(this).addClass('layui-btn-disabled');
             let s = 59;
             $(this).text(s + "秒后重试...");
             let inter = setInterval(() => {
                 s--;
                 if (s === 0) {
                     $(this).removeClass("layui-btn-disabled").text("获取验证码")
                     clearInterval(inter);
                 } else {
                     $(this).text((s < 10 ? '0' + s : s) + "秒后重试...");
                 }

             }, 1000);
             http.get('blog/send', {phone}, function (res) {
                 layer.msg('发送成功！', {icon: 6});
                 codeUUID = res.msg
             })
         });*/
    })

    function changeTab(id) {
        element.tabChange('loginTab', id)
    }


    function generateUUID() {
        let d = new Date().getTime();
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            let r = (d + Math.random() * 16) % 16 | 0;
            d = Math.floor(d / 16);
            return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
        });
    }

    function getKaptcha() {
        uuid = generateUUID();
        const url = 'kaptcha.jpg?uuid=' + uuid;
        $("#kaptchaImage").prop('src', url);
    }

    function forget() {
        /*获取当前窗口的标记值*/
        const index = parent.layer.getFrameIndex(window.name);
        /*关闭窗口*/
        parent.layer.close(index);
        parent.layer.open({
            type: 2,
            content: "page/forget",
            area: ["400px", "500px"],
            title: "忘记密码"
        })
    }

    function getLoginInfo() {
        let loginInfo;
        $.ajax({
            url: "login/info",
            method: "get",
            headers,
            async: false,
            dataType: "json",
            success(res) {
                if (res.code === 200) {
                    loginInfo = {...res.data}
                }
            },
            error(e) {
                layer.alert('与服务器断开连接...');
                console.log(e)
            }
        })
        return loginInfo;
    }


    function setHeader() {
        const vipInfo = getLoginInfo();
        if (vipInfo) {
            $("#vipName").text(vipInfo.nickname)
            $("#vipAvatar").prop('src', 'static/upload/' + vipInfo.avatar)
            $(".user .avatar").css('display', 'flex');
            $(".user .login").css('display', 'none');
        } else {
            $(".user .avatar").css('display', 'none');
            $(".user .login").css('display', 'flex');
        }
    }
</script>
</html>
